<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>

  <style>
    * {
      margin: 0;
      padding: 0;
    }

    ul,
    li {
      list-style: none;
    }

    ul {
      height: 40px;
      margin: 30px auto;
      border-bottom: 3px solid pink;
      width: 1000px;
      position: relative;
    }

    li {
      height: 100%;
      width: 100px;
      float: left;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
    }

    ul>span {
      width: 100px;
      height: 3px;
      position: absolute;
      left: 0;
      bottom: -3px;
      background-color: red;
    }

    ul>p {
      width: 100px;
      height: 40px;
      background-color: skyblue;
      color: #fff;
      position: absolute;
      top: 0;
      left: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      border-bottom: 2px solid red;
    }
  </style>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>

<body>
  <ul>
    <li>第1个选项</li>
    <li>第2个选项</li>
    <li>第3个选项</li>
    <li>第4个选项</li>
    <li>第5个选项</li>
    <li>第6个选项</li>
    <li>第7个选项</li>
    <li>第8个选项</li>
    <li>第9个选项</li>
    <li>第10个选项</li>
    <p>第1个选项</p>
  </ul>
</body>

</html>
<script src='https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js'></script>
<script>
  //声明一个开关,用于标记是否点击了
  let isClick = false;
  //思路:
  //鼠标移入到Li的时候 获取当前li的方位,复制给p标签的left就可以了
  $("ul").on("mouseenter", "li", function () {
    //滑动的时候 让isClick为false 这样p标签就能回去了
    isClick = false;
    // console.log($(this).position().left)
    $("p").stop().animate({ left: $(this).position().left }, 200).text($(this).text())
  })

  $("ul").mouseleave(function () {
    if (isClick) {
      //点击了p标签
      //什么也不干
      return
    }
    $("p").animate({
      left: 0
    }, 1000).text("第1个选项")
  })

  //点击保留位置 注意注册给p标签 因为p浮在了li上面 点不到li

  $("p").click(function () {
    // console.log(123)
    isClick = true;
  })

</script>